package com.ts.api.module.api.entity.bo;

import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.encode.HexUtils;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import lombok.Data;

/**
 *  API消息加密业务实体
 *  @author Pan
 */
@Data
public class ApiEncryptBO {

    /** 待加密内容 */
    private String content;
    /** ApiKey */
    private ApiKeyPO cacheKey;
    /** 密文内容 */
    private String ciphertext;

    public ApiEncryptBO() {
        super();
    }

    public ApiEncryptBO(String ciphertext, ApiKeyPO cacheKey) {
        this.ciphertext = ciphertext;
        this.cacheKey = cacheKey;
    }

    /**
     * API加密aes方法
     *
     * @return ApiDecryptBO
     */
    ApiEncryptBO aes() {
        AES ecb = AES.createEcb(HexUtils.decode(cacheKey.getApiSecretKey()));
        try {
            this.ciphertext = ecb.encryptToHex(content);
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_DECRYPT_ERROR, e.getMessage());
        }
        return this;
    }

    /**
     * API加密aes
     *
     * @param  ciphertext
     * @param  cacheKey
     * @return ApiDecryptBO
     */
    public static ApiEncryptBO aes(String ciphertext, ApiKeyPO cacheKey) {
        return new ApiEncryptBO(ciphertext, cacheKey).aes();
    }
}
